package com.example.springbooteasycode.bootsocket;

import java.io.IOException;
import java.net.Socket;

public class SocketClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("127.0.0.1", 8888)) { // 连接服务端 8888 端口
            // 发送请求数据
            String request = "Hello, Socket Server!";
            socket.getOutputStream().write(request.getBytes());
            socket.getOutputStream().flush();
            System.out.println("客户端发送请求：" + request);

            // 接收响应
            byte[] response = new byte[1024];
            int len = socket.getInputStream().read(response);
            System.out.println("客户端收到响应：" + new String(response, 0, len));
        } catch (IOException e) {
            System.err.println("客户端异常：" + e.getMessage());
        }
    }
}
